package com.simple.dispatcher.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.simple.dispatcher.service.DispatcherService;
import com.simple.dispatcher.servlet.utils.JSONUtils;
import com.simple.dispatcher.servlet.utils.RequestParamUtils;

@Controller
public class NewUserFilterServlet {
	
	@Autowired
	private DispatcherService dispatcherService;
	
	/**
	 * 获取uid和表单数据，并生成JSON格式filter并交由dispatchService进行处理
	 */
	@RequestMapping("/addfilter.do")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)	throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		long userId = RequestParamUtils.getLongValue(req, "uid", 0L);
		String[] locations = RequestParamUtils.getStringValues(req, "location", null);
		String[] jobtitles = RequestParamUtils.getStringValues(req, "jobtitle", null);
		String[] industries = RequestParamUtils.getStringValues(req, "industry", null);
		long salary = RequestParamUtils.getLongValue(req, "salary", 0L);
		String company = RequestParamUtils.getStringValue(req, "company", "");
		String[] types = RequestParamUtils.getStringValues(req, "job_type", null);
		
		String filter = JSONUtils.generateJobMetaOrFilter(industries, jobtitles, locations, salary, company, types, false);
		if(userId != 0 && StringUtils.isNotEmpty(filter)) {
			dispatcherService.addUserFilter(userId, filter);
			resp.setStatus(HttpServletResponse.SC_OK);
			return;
		}
		resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
	}
}
